﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CXMLStudioIDE.DataAccessLayer;
using CXMLStudioIDE.Common;
using CXMLStudioIDE.Common.Entities;

namespace CXMLStudioIDE
{
    public partial class AddEntityForm : Form
    {
        private MainForm frm_parent;
        private Solution m_solution;

        public NewProjectForm(Form parent, Solution solution)
        {
            InitializeComponent();
            frm_parent = parent as MainForm;
            pnlNewSolution.Visible = true;
            CXMLStudioIDE.DataAccessLayer.CXMLStudioDAL dal = new CXMLStudioDAL();
            dsScope = dal.GetScope();
            cmbScope.DisplayMember = dsScope.Tables[0].Columns[1].ColumnName.ToString();
            cmbScope.ValueMember = dsScope.Tables[0].Columns[0].ColumnName.ToString();
            cmbScope.DataSource = dsScope.Tables[0];
            m_solution = solution;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void cmbScope_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbContext.DataSource = null;
            cmbContext.Items.Clear();
            if (cmbScope.SelectedIndex > 0)
            {
                CXMLStudioIDE.DataAccessLayer.CXMLStudioDAL dal = new CXMLStudioDAL();
                dsContext = dal.GetContextInScope((int)cmbScope.SelectedValue);
                cmbContext.DisplayMember = dsContext.Tables[0].Columns[1].ColumnName.ToString();
                cmbContext.ValueMember = dsContext.Tables[0].Columns[0].ColumnName.ToString();
                cmbContext.DataSource = dsContext.Tables[0];
            }
        }

        private void cmbContext_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbBusinessEntity.DataSource = null;
            cmbBusinessEntity.Items.Clear();
            if ((int)cmbContext.SelectedValue > 0)
            {
                CXMLStudioIDE.DataAccessLayer.CXMLStudioDAL dal = new CXMLStudioDAL();
                dsBusinessEntity = dal.GetBusinessEntitiesInContext((int)cmbContext.SelectedValue);
                cmbBusinessEntity.DisplayMember = dsBusinessEntity.Tables[0].Columns[1].ColumnName.ToString();
                cmbBusinessEntity.ValueMember = dsBusinessEntity.Tables[0].Columns[0].ColumnName.ToString();
                cmbBusinessEntity.DataSource = dsBusinessEntity.Tables[0];
            }
        }

        private void lbTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtProjectName.Text.Length == 0)
            {
                MessageBox.Show("Please enter a Project name", "CXMLStudioIDE - Warning");
            }
            if (lbTemplates.Items.Count == 0)
            {
                MessageBox.Show("Please assign Business entities","CXMLStudioIDE - Warning");
            }
            else
            {
                if (m_solution != null && m_solution.Projects == null)
                {
                    //If solution exists with no project list
                    //Then create a project list for solution
                    frm_parent.CreateSolutionProjectList();
                }
                if(m_solution == null) {
                    frm_parent.CreateProject();
                }

                Project p = new Project();
                p.Name = txtProjectName.Text;

                BusinessEntityCollection be_col = new BusinessEntityCollection();
                //Create a list of business entities
                foreach (BusinessEntity b in lbTemplates.Items)
                {
                    be_col.Add(b);
                }
                if (be_col.Count > 0)
                {
                    p.AddBusinessEntitiesToProject(be_col);
                }

                if (m_solution != null && m_solution.Projects != null)
                {
                    CXMLStudioDAL dal = new CXMLStudioDAL();
                    DataSet ds = dal.SaveNewProject(frm_parent.m_solution.ID, p.Name, dal.GetCXMLUserIdByName(frm_parent.m_CXMLUser), 0);
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        p.ID = (int)ds.Tables[0].Rows[0].ItemArray[0];
                        //TODO: Associate Entities to Project in database.
                        foreach (BusinessEntity b in be_col)
                        {
                            dal.AddExistingEntityVersionToProject(p.ID, b.EntityTypeVersionID);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Could not save new Solution","CXMLStudioIDE Error!");
                    }
                    frm_parent.m_solution.AddProjectToSolution(p);
                }
                else
                {
                    frm_parent.m_project = p;
                    frm_parent.EnabledMenuItem("menuItemCloseProject", true);
                }
                //frm_parent.DsBusinessEntityCode = this.dsBusinessEntity;
                frm_parent.UpdateMainForm();
                this.Dispose();
            }
        }

        private void lblTemplates_Click(object sender, EventArgs e)
        {

        }

        private void cmbBusinessEntity_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbVersion.DataSource = null;
            cmbVersion.Items.Clear();
            if ((int)cmbBusinessEntity.SelectedValue > 0)
            {
                CXMLStudioIDE.DataAccessLayer.CXMLStudioDAL dal = new CXMLStudioDAL();
                dsVersion = dal.GetBusinessEntityInContextVersion((int)cmbBusinessEntity.SelectedValue);
                cmbVersion.DisplayMember = dsVersion.Tables[0].Columns[0].ColumnName.ToString();
                cmbVersion.ValueMember = dsVersion.Tables[0].Columns[1].ColumnName.ToString();
                cmbVersion.DataSource = dsVersion.Tables[0];
            }
        }

        private void btnAssign_Click(object sender, EventArgs e)
        {
            BusinessEntity be = new BusinessEntity();
            be.ScopeID = (int)cmbScope.SelectedValue;
            be.ScopeName = ((DataRowView)cmbScope.SelectedItem).Row.ItemArray[1].ToString();
            be.ContextID = (int)cmbContext.SelectedValue;
            be.ContextName = ((DataRowView)cmbContext.SelectedItem).Row.ItemArray[1].ToString();
            be.Name = ((DataRowView)cmbBusinessEntity.SelectedItem).Row.ItemArray[1].ToString();
            be.ID = (int)cmbBusinessEntity.SelectedValue;
            be.EntityVersionID = (int)cmbVersion.SelectedValue;
            be.EntityTypeVersionID = Convert.ToInt32(((DataRowView)cmbVersion.SelectedItem).Row.ItemArray[2].ToString());
            be.Version = ((DataRowView)cmbVersion.SelectedItem).Row.ItemArray[0].ToString();
            lbTemplates.Items.Add(be);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lbTemplates.SelectedIndex >= 0)
            {
                lbTemplates.Items.RemoveAt(lbTemplates.SelectedIndex);
            }
            else {
                MessageBox.Show("Select an item to remove", "CXMLStudioIDE message");
            }
        }
    }

}
